var upload={
	error:function(msg){
		common_ops.alert(msg);
	},
	success:function(image_key){
		// alert(image_key);
		$html='<img src="'+common_ops.buildPicUrl('book',image_key)+'">'+
		'<span class="fa fa-times-circle del del_image" data='+image_key+'><i></i></span>';
		if($(".upload_pic_wrap .pic-each").size()>0){
			$(".upload_pic_wrap .pic-each").html($html);
		}else{
			$(".upload_pic_wrap").append('<span class="pic-each">'+$html+'</span>');
		}
		book_ops.delete_img();
	}	
};
var book_ops={
	init:function(){
		this.eventBind();
	},
	eventBind:function(){
		//点击保存事件
		$(".save").click(function(){

			var btn_target=$(this);
			var bucket=$("input[name=bucket]").val();
			
			var name=$("input[name=name]").val();//图书名称
			var id=$("input[name=id]").val();//id
			var name_target=$("input[name=name]");

			var cat_id=$("#cat_id").val();//分类id
			var cat_id_target=$("#cat_id");
			
			var price=$("input[name=price]").val();//价格
			var price_target=$("input[name=price]");

			var editor=$("#editor").val();//描述
			var editor_target=$("#editor");

			var stock=$("input[name=stock]").val();//库存
			var stock_target=$("input[name=stock]");

			var tags=$("input[name=tags]").val();//库存
			var tags_target=$("input[name=tags]");
			var image_key=$(".del_image").attr("data");
			console.log(name+cat_id+price+editor+stock+tags);
			if(btn_target.hasClass("disabled")){
				common_ops.alert("正在处理,请不要重复点击!");
				return;
			};

			if(cat_id<1){
				
				common_ops.tip("请选择分类",cat_id_target);
				return;

			}
			if($.trim(name).length<1){
				common_ops.tip("图书名称不能为空",name_target);
				return;

			}
			if($.trim(price).length<1){
				common_ops.tip("请输入图书售价",price_target);
				return;

			}	
			if($.trim(editor).length<1){
				common_ops.tip("请输入图书描述",editor_target);
				return;

			}
			if($.trim(stock).length<1){
				common_ops.tip("请输入图书库存",stock_target);
				return;

			}
			if($.trim(tags).length<1){
				common_ops.tip("请输入图书标签",tags_target);
				return;

			}
			btn_target.addClass("disabled");
			$.ajax({
				url:common_ops.buildWebUrl("/book/set"),
				type:'POST',
				dataType:"json",
				data:{
					cat_id:cat_id,
					name:name,
					price:price,
					editor:editor,
					stock:stock,
					tags:tags,
					image_key:image_key,
					id:id,
					bucket:bucket,
				},
				success:function(res){
					btn_target.removeClass("disabled");
					callback=null;
					if(res.code==200){
						 callback=function(){
						 	window.location.href=window.location.href;
						 };
					}
					common_ops.alert(res.msg,callback);
				},
			});
		});
		//点击删除事件
		$(".remove").click(function(){
			var id=$(this).attr("data");
			
			$.ajax({
				url:common_ops.buildWebUrl('/book/cat_del'),
				type:"POST",
				dataType:"json",
				data:{
					id:id,
				},
				success:function(res){

					callback=null;
					if(res.code==200){
						 callback=function(){
						 	window.location.href=window.location.href;
						 };
					}
					common_ops.alert(res.msg,callback);
				}
			});
		});
		//点击上传图片事件
		$(".upload_pic_wrap input[name=pic]").change(function(){
			$(".upload_pic_wrap").submit();
		});

	},
	delete_img:function(){
		$(".upload_pic_wrap .del_image").unbind().click(function(){
			$(this).parent().remove();
		});
	},
};
$(document).ready(function(){
	book_ops.init();
});